package io.github.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.github.dao.SysCallStatisticsDao;
import io.github.dao.SysCalllogsDao;
import io.github.entity.SysCallStatisticsEntity;
import io.github.entity.SysCallogsEntity;
import io.github.pojo.CallCountDto;
import io.github.service.SysCallStatictiscService;
import io.github.service.SysCalllogsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * @author: weil
 * @date: 2019/9/18 20:33
 * @version: 1.0
 * @description:
 */
@Service
public class SysCallStatictiscServiceImpl extends ServiceImpl<SysCallStatisticsDao, SysCallStatisticsEntity> implements SysCallStatictiscService {

    @Resource
    SysCallStatisticsDao dao;

    @Override
    public List<SysCallStatisticsEntity> listStatistics() {
        List<SysCallStatisticsEntity> list = dao.list();

        Map<String, SysCallStatisticsEntity> resultMap = new HashMap();
        List<SysCallStatisticsEntity> list2 = new ArrayList<>();

        for(SysCallStatisticsEntity entity:list) {
            //entityTemp 为原来的一个数据
            SysCallStatisticsEntity entityTemp = resultMap.putIfAbsent(entity.getNumberSection(), entity);

            if(null !=entityTemp) {
                entityTemp.setCallCount(entity.getCallCount()+entityTemp.getCallCount());
                if(entity.getHangupReason().equals("USER_BUSY")) {
                    entityTemp.setNotReceive(entity.getReasonCount());
                } else if(entity.getHangupReason().equals("NORMAL_CLEARING")){
                    entityTemp.setSuccess(entity.getReasonCount());
                } else {
                    entityTemp.setFail(entity.getReasonCount()+entityTemp.getFail());
                }
                entityTemp.setRecordSecondTime(entity.getRecordSecondTime()+entityTemp.getRecordSecondTime());
            }
        }

        Iterator it = resultMap.entrySet().iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            SysCallStatisticsEntity entity = (SysCallStatisticsEntity) entry.getValue();
            entity.setRecordMinTime(String.format("%.2f",((float)entity.getRecordSecondTime())/60));
            entity.setRecordHourTime(String.format("%.2f",((float)entity.getRecordSecondTime())/3600));
            if(null!=entity.getSuccess()) {
                entity.setRate(String.format("%.1f",((float)entity.getSuccess()/entity.getCallCount()*100)) +"%");
            }
            list2.add(entity);
        }

        return list2;
    }


}
